package com.mumu.muPictureBackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mumu.muPictureBackend.exception.BusinessException;
import com.mumu.muPictureBackend.exception.ErrorCode;
import com.mumu.muPictureBackend.exception.ThrowUtils;
import com.mumu.muPictureBackend.mapper.AdminMapper;
import com.mumu.muPictureBackend.model.dto.user.UserCreateRequest;
import com.mumu.muPictureBackend.model.dto.user.UserQueryRequest;
import com.mumu.muPictureBackend.model.entity.User;
import com.mumu.muPictureBackend.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, User> implements AdminService {

    /**
     * 创建用户
     * 思路:请求参数已经封装到了该类中 然后只需要copy到user中即可
     *
     * @param userCreateRequest
     * @return
     */
    @Override
    public Long createUser(UserCreateRequest userCreateRequest) {
        User user = new User();
        BeanUtil.copyProperties(userCreateRequest, user);
        //如果密码为空 就设置一个默认密码
        if (StrUtil.isBlank(userCreateRequest.getPassWord())) {
            userCreateRequest.setPassWord("12345678");
        }
        //别忘了密码加密!!!
        user.setUserPassword(getEncryptPassword(userCreateRequest.getPassWord()));//传过去该密码的字节码然后加密的
        ThrowUtils.throwIf(!save(user), ErrorCode.OPERATION_ERROR, "保存失败 换个账号试试 可能账号重复");
        return user.getId();
    }

    /**
     * 根据id删除用户
     *
     * @param userId
     */
    @Override
    public Boolean deleteById(Long userId) {
        if (userId == null || userId <= 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        boolean b = removeById(userId);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR, "在数据库中删除失败 id错误吧 用户不存在");
        return b;
    }

    /**
     * 根据id获取用户
     *
     * @param userId
     * @return
     */
    @Override
    public User getUserById(Long userId) {
        ThrowUtils.throwIf(userId <= 0, ErrorCode.PARAMS_ERROR);
        User user = getById(userId);
        ThrowUtils.throwIf(user == null, ErrorCode.OPERATION_ERROR, "数据库中查不到该用户");
        return user;
    }


    /**
     * 获取user的查询条件 说白了就是把条件封装到了queryWrapper中 然后调用直接用
     *
     * @param request
     * @return
     */
    @Override
    public QueryWrapper<User> getQueryWrapper(UserQueryRequest request) {
        Long id = request.getId();
        String userAccount = request.getUserAccount();
        String userName = request.getUserName();
        String userRole = request.getUserRole();
        String userProfile = request.getUserProfile();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(ObjUtil.isNotNull(id), "id", id)
                .eq(StrUtil.isNotBlank(userAccount), "user_account", userAccount)
                .like(StrUtil.isNotBlank(userName), "user_name", userName)
                .like(StrUtil.isNotBlank(userRole), "user_role", userRole)
                .like(StrUtil.isNotBlank(userProfile), "user_profile", userProfile)
                .orderBy(StrUtil.isNotBlank(sortField), sortOrder.equals("ascend"), sortField);
        //第一个条件:是否应用这个条件(如果指定的排序字段不为空) 第二个条件:是升序还是降序 如果是ascend就是升序 否则就是降序 第三个就是要排序的字段
        return queryWrapper;
    }


    /**
     * 密码加密
     */
    public String getEncryptPassword(String password) {
        final String SALT = "MuMu";
        return DigestUtils.md5DigestAsHex((SALT + password).getBytes());
    }


}
